package interpreter;

import grammar.FunParser;

import java.util.ArrayList;
import java.util.HashMap;

public class FunctionStorage {
    private HashMap<String, Function> functions = new HashMap<String, Function>();
    private TestFunVisitor visitor;

    public FunctionStorage(TestFunVisitor visitor) {
        this.visitor = visitor;
    }

    public void addFunction(FunParser.LetContext ctx) {
        Function f = new Function(ctx, visitor);
        if (functions.containsKey(f.getName())) {
            System.err.println("function named " + f.getName() + " already exists!");
        } else {
            functions.put(f.getName(), f);
            System.err.println("Function storage: add function " + f.getName());
        }
    }

    public Integer callFunction(String name, ArrayList<Integer> args) {
        if (functions.containsKey(name)) {

            Integer result = functions.get(name).call(args);
            return result;
        } else {
            System.err.println("Unknown function named " + name);
            return 0;
        }
    }
}
